% This file makes a table based on func_wrapper_shortlong
% The idea in that file was to see if the gains from completing choice sets
% varied with whether teachers were on the short or long side of the market

clear all;
close all;
clc

cd E:\ReplicateBuild\02_code\03_matlab

store_suffix='monthbymonth';


%%%
% timing of vacancy posting and teachers entering
%%%

base=['app_posting_timing.mat'];
folder='../../01_data/05_temp/model/';
savefile=fullfile(folder, base);

basedata=open(savefile); 
app_sh=basedata.app_sh;
post_sh=basedata.post_sh;
post_titleI_sh=basedata.post_titleI_sh;
post_nontitleI_sh=basedata.post_nontitleI_sh;


%%%
% restricted timing
%%%
base=['alloc_teach_est_princ_est_restr_',store_suffix,'.mat'];
basefilename=join(base);
folder='../../01_data/05_temp/model/';
savefile=fullfile(folder, basefilename);


basedata=open(savefile);

balanced_restr=basedata.balanced;


%%%
% restricted timing for non-Title I, unrestricted for title I 
%%%
base=['alloc_teach_est_princ_est_nontitleI_restr_',store_suffix,'.mat'];
basefilename=join(base);
folder='../../01_data/05_temp/model/';
savefile=fullfile(folder, basefilename);


basedata=open(savefile);

balanced_nontitleI_restr=basedata.balanced;


%%%
% restricted timing, noisy
%%%
base=['alloc_teach_est_princ_noisy_restr_',store_suffix,'.mat'];
basefilename=join(base);
folder='../../01_data/05_temp/model/';
savefile=fullfile(folder, basefilename);


basedata=open(savefile);

balanced_noisy=basedata.balanced;

%%%
% unrestricted timing
%%%
base=['alloc_teach_est_princ_est_unrestr_',store_suffix,'.mat'];
basefilename=join(base);
folder='../../01_data/05_temp/model/';
savefile=fullfile(folder, basefilename);


basedata=open(savefile);

balanced_unrestr=basedata.balanced;


%%%
% restricted timing, principal max va 
%%%
base=['alloc_teach_est_princ_va_restr_',store_suffix,'.mat'];
basefilename=join(base);
folder='../../01_data/05_temp/model/';
savefile=fullfile(folder, basefilename);


basedata=open(savefile);

balanced_va=basedata.balanced;


%%%
% restricted timing, teach max ndis
%%%
base=['alloc_teach_ndis_princ_est_restr_',store_suffix,'.mat'];
basefilename=join(base);
folder='../../01_data/05_temp/model/';
savefile=fullfile(folder, basefilename);


basedata=open(savefile);

balanced_ndis=basedata.balanced;



%%%
% restricted timing, teach max ndis, principal max va
%%%
base=['alloc_teach_ndis_princ_va_restr_',store_suffix,'.mat'];
basefilename=join(base);
folder='../../01_data/05_temp/model/';
savefile=fullfile(folder, basefilename);


basedata=open(savefile);

balanced_both=basedata.balanced;


%%%  
%  6. status quo, principal max VA, unrestricted choice sets, also compute first best 
%%%

base=['alloc_teach_est_princ_va_unrestr_',store_suffix,'.mat'];
basefilename=join(base);
folder='../../01_data/05_temp/model/';
savefile=fullfile(folder, basefilename);


basedata=open(savefile);

balanced_fb=basedata.balanced;







%%
% MATLAB script to produce a LaTeX table with dynamic data

% Specify the directory path where you want to save the .tex file
save_path = 'E:\ReplicateBuild\03_output\tables\';

% Complete file path with file name
file_name = [save_path 'monthbymonth_postingdate.tex'];

% Open a file to write
fileID = fopen(file_name,'w');

% Check if the file was opened successfully
if fileID == -1
    error('File could not be opened, check if the path is correct and writable.');
end

%[va_disadv va_adv fracdisadv_matched fracdisadv_unmatched va_disadv_unmatch];
% Write the table header and begin the tabular environment
fprintf(fileID, ' \\begin{tabular}{lccccccccc} \\hline \\noalign{\\smallskip}  Description & VA disadv. & VA adv. & \\multicolumn{3}{c}{VA disadv.: By period} & \\multicolumn{3}{c}{VA adv.: By period}  \\noalign{\\smallskip}  \\\\\\hline\n');
fprintf(fileID, ' \\noalign{\\smallskip}& \\multicolumn{7}{l}{\\textbf{A. Clearing the market in one period   }} \\noalign{\\smallskip} \\\\\\hline\n');
fprintf(fileID, '\\noalign{\\smallskip} & &&  \\multicolumn{1}{c}{1} & \\multicolumn{1}{c}{2} & \\multicolumn{1}{c}{3} &  \\multicolumn{1}{c}{1} & \\multicolumn{1}{c}{2} & \\multicolumn{1}{c}{3} \\\\\n');
fprintf(fileID, 'Status quo & %.3f & %.3f & %.3f & %.3f & %.3f & %.3f & %.3f & %.3f  \\\\\n', balanced_restr(1,9:16) );
fprintf(fileID, '\\text{    } \\textit{Title I} & %.3f & %.3f & %.3f & %.3f & %.3f & %.3f & %.3f & %.3f  \\\\\n', balanced_restr(1,25:32));
fprintf(fileID, '\\text{    } \\textit{Non Title I} & %.3f & %.3f & %.3f & %.3f & %.3f & %.3f & %.3f & %.3f  \\\\\n', balanced_restr(1,33:40));
fprintf(fileID, 'All options (timing) & %.3f & %.3f & %.3f & %.3f & %.3f & %.3f & %.3f & %.3f  \\noalign{\\smallskip} \\\\\\hline\n', balanced_unrestr(1,9:16));
%fprintf(fileID, 'Noisy hiring & %.3f & %.3f & %.3f & %.3f & %.3f & %.3f & %.3f & %.3f  \\\\\\hline\n', balanced_noisy(1,9:16));
%fprintf(fileID, 'Teachers N Dis & %.3f & %.3f  & %.3f & %.3f & %.3f & %.3f & %.3f & %.3f   \\\\\\hline\n', balanced_ndis(1,9:16));
%fprintf(fileID, 'Principals VA & %.3f & %.3f  & %.3f & %.3f & %.3f & %.3f & %.3f & %.3f  \\\\\\hline\n', balanced_va(1,9:16));
%fprintf(fileID, 'Previous two & %.3f & %.3f  & %.3f & %.3f & %.3f & %.3f & %.3f & %.3f  \\\\\\hline\n', balanced_both(1,9:16));
%fprintf(fileID, 'First best & %.3f & %.3f  & %.3f & %.3f & %.3f & %.3f & %.3f & %.3f   \\\\\\hline\n', balanced_fb(1,9:16));
fprintf(fileID, '\\noalign{\\smallskip} & \\multicolumn{7}{l}{\\textbf{B. Clearing the market in three periods   }} \\noalign{\\smallskip} \\\\\\hline\n');
fprintf(fileID, '\\noalign{\\smallskip}& &&  \\multicolumn{1}{c}{1} & \\multicolumn{1}{c}{2} & \\multicolumn{1}{c}{3} &  \\multicolumn{1}{c}{1} & \\multicolumn{1}{c}{2} & \\multicolumn{1}{c}{3} \\\\\n');
fprintf(fileID, 'Status quo & %.3f & %.3f & %.3f & %.3f & %.3f & %.3f & %.3f & %.3f   \\noalign{\\smallskip} \\\\\\hline\n', balanced_restr(1,17:24));
%fprintf(fileID, 'Noisy hiring & %.3f & %.3f & %.3f & %.3f & %.3f & %.3f & %.3f & %.3f  \\\\\\hline\n', balanced_noisy(1,17:24));
%fprintf(fileID, 'Teachers N Dis & %.3f & %.3f  & %.3f & %.3f & %.3f & %.3f & %.3f & %.3f   \\\\\\hline\n', balanced_ndis(1,17:24));
%fprintf(fileID, 'Principals VA & %.3f & %.3f  & %.3f & %.3f & %.3f & %.3f & %.3f & %.3f  \\\\\\hline\n', balanced_va(1,17:24));
%fprintf(fileID, 'Previous two & %.3f & %.3f  & %.3f & %.3f & %.3f & %.3f & %.3f & %.3f  \\\\\\hline\n', balanced_both(1,17:24));
%fprintf(fileID, 'First best & %.3f & %.3f  & %.3f & %.3f & %.3f & %.3f & %.3f & %.3f   \\\\\\hline\n', balanced_fb(1,17:24));
%fprintf(fileID, '& \\multicolumn{7}{l}{\\textbf{C. Non-Title I restricted timing, Title I unrestricted}} \\\\\\hline\n');
%fprintf(fileID, 'Status quo & %.3f & %.3f & %.3f & %.3f & %.3f & %.3f & %.3f & %.3f  \\\\\\hline\n', balanced_nontitleI_restr(1,9:16));
%fprintf(fileID, '\\text{    } \\textit{Title I} & %.3f & %.3f & %.3f & %.3f & %.3f & %.3f & %.3f & %.3f  \\\\\\hline\n', balanced_nontitleI_restr(1,25:32));
%fprintf(fileID, '\\text{    } \\textit{Non Title I} & %.3f & %.3f & %.3f & %.3f & %.3f & %.3f & %.3f & %.3f  \\\\\\hline\n', balanced_nontitleI_restr(1,33:40));
fprintf(fileID, ' \\noalign{\\smallskip} &  \\multicolumn{7}{l}{\\textbf{C. Share of vacancy posting and teacher entry by sub-period}}  \\noalign{\\smallskip} \\\\\\hline\n');
fprintf(fileID, ' \\noalign{\\smallskip} &   \\multicolumn{1}{c}{1} & \\multicolumn{1}{c}{2} & \\multicolumn{1}{c}{3} \\\\\n');
fprintf(fileID, 'Vacancy posting & %.2f & %.2f & %.2f   \\\\\n', post_sh);
fprintf(fileID, '\\text{   } \\textit{Title I} & %.2f & %.2f & %.2f   \\\\\n', post_titleI_sh);
fprintf(fileID, '\\text{   }  \\textit{Non-Title I} & %.2f & %.2f & %.2f   \\\\\n', post_nontitleI_sh);
fprintf(fileID, 'First application & %.2f & %.2f & %.2f   \\\\\n', app_sh);
% End the tabular environment and close the table
fprintf(fileID, ' \\noalign{\\smallskip} \\hline \\end{tabular}\n');

% Close the file
fclose(fileID);

% Notify the user
fprintf('LaTeX table saved to %s\n', file_name);

%%
%%
% MATLAB script to produce a LaTeX table with dynamic data
% 
% % Specify the directory path where you want to save the .tex file
% save_path = 'E:\ReplicateBuild\03_output\tables\';
% 
% % Complete file path with file name
 file_name = [save_path 'monthbymonth_entrydate.tex'];
% 
% % Open a file to write
 fileID = fopen(file_name,'w');
% 
% % Check if the file was opened successfully
 if fileID == -1
     error('File could not be opened, check if the path is correct and writable.');
 end
% 
% %[va_disadv va_adv fracdisadv_matched fracdisadv_unmatched va_disadv_unmatch];
% % Write the table header and begin the tabular environment
 fprintf(fileID, ' \\begin{tabular}{lcccc} \\hline \\noalign{\\smallskip}  \\noalign{\\smallskip} \\multicolumn{1}{l}{Description} & \\multicolumn{1}{c}{Overall} & \\multicolumn{3}{c}{By period} \\noalign{\\smallskip}  \\\\\\hline\n');
 fprintf(fileID, '& \\multicolumn{5}{l}{\\textbf{Clearing the market in one period   }} \\noalign{\\smallskip} \\\\\\hline\n');
 fprintf(fileID, '\\noalign{\\smallskip} & &   \\multicolumn{1}{c}{1} & \\multicolumn{1}{c}{2} & \\multicolumn{1}{c}{3} \\noalign{\\smallskip} \\\\\\hline\n');
 fprintf(fileID, 'Status quo & %.1f & %.1f & %.1f & %.1f   \\\\\n', balanced_restr(1,41:44));
 fprintf(fileID, 'All options (timing) & %.1f & %.1f & %.1f & %.1f  \\\\\n', balanced_unrestr(1,41:44));
 %fprintf(fileID, 'Noisy hiring & %.1f & %.1f & %.1f & %.1f   \\\\\\hline\n', balanced_noisy(1,41:44));
% %fprintf(fileID, 'Teachers N Dis & %.1f & %.1f & %.1f & %.1f    \\\\\\hline\n', balanced_ndis(1,41:44));
% %fprintf(fileID, 'Principals VA & %.1f & %.1f & %.1f & %.1f   \\\\\\hline\n', balanced_va(1,41:44));
% %fprintf(fileID, 'Previous two & %.1f & %.1f & %.1f & %.1f  \\\\\\hline\n', balanced_both(1,41:44));
%fprintf(fileID, '& \\multicolumn{5}{l}{\\textbf{B. Clearing the market in three periods   }} \\\\\\hline\n');
% %fprintf(fileID, '& &  \\multicolumn{1}{c}{1} & \\multicolumn{1}{c}{2} & \\multicolumn{1}{c}{3}  \\\\\\hline\n');
% %fprintf(fileID, 'Status quo & %.1f & %.1f & %.1f & %.1f  \\\\\\hline\n', balanced_restr(1,45:48));
% %fprintf(fileID, 'Noisy hiring & %.1f & %.1f & %.1f & %.1f   \\\\\\hline\n', balanced_noisy(1,45:48));
% %fprintf(fileID, 'Teachers N Dis & %.1f & %.1f & %.1f & %.1f    \\\\\\hline\n', balanced_ndis(1,45:48));
% %fprintf(fileID, 'Principals VA & %.1f & %.1f & %.1f & %.1f   \\\\\\hline\n', balanced_va(1,45:48));
% %fprintf(fileID, 'Previous two & %.1f & %.1f & %.1f & %.1f   \\\\\\hline\n', balanced_both(1,45:48));
% % End the tabular environment and close the table
 fprintf(fileID, '\\noalign{\\smallskip} \\hline \\end{tabular}\n');
% 
% % Close the file
 fclose(fileID);
% 
% % Notify the user
 fprintf('LaTeX table saved to %s\n', file_name);
